<template>
  <div class="app-container">
    <el-form :inline="true" :model="form" class="demo-form-inline">
      <el-form-item>
        <el-button type="success" @click="handleAdd()">添加公告</el-button>
      </el-form-item>
      <el-form-item label="标题">
        <el-input v-model="form.title" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="选择分类">
        <el-select v-model="form.typeId" placeholder="请选择分类">
          <el-option v-for="item in list" :key="item.id" :label="item.typeName" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getArticleList">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="tableDatas" element-loading-text="Loading" fit highlight-current-row>
      <el-table-column align="center" label="ID" prop="id" width="100px"></el-table-column>
      <el-table-column align="center" label="标题" prop="title" width="250"></el-table-column>
      <el-table-column label="封面">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.cover" :preview-src-list="[scope.row.cover]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column align="center" label="分类" prop="typeName" width="150"></el-table-column>
      <el-table-column align="center" label="发布时间" prop="publishDate" width="250"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="form.page"
      :page-sizes="[5, 10, 20, 40]" :page-size="form.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-dialog title="公告添加&编辑" :visible.sync="dialogFormVisible">
      <!-- 绑定key值，保证每次打开弹窗时，都会重新渲染 -->
      <add :key="dialogFormVisible" :list="list" @done="done" :info="info"></add>
    </el-dialog>
  </div>
</template>

<script>
import { articleList, articleCategoryList, articleDelete } from '@/api/article'
import add from './components/add.vue'
export default {
  components: { add },
  name: 'ArticleIndex',
  data() {
    return {
      list: [],//cates
      listLoading: false,
      form: {
        page: 1,
        pageSize: 5,
        title: '',
        typeId: '',
      },
      tableDatas: [],
      total: 0,
      info: {},
      dialogFormVisible: false,
    };
  },
  methods: {
    getArticleList() {
      articleList(this.form).then(res => {
        this.tableDatas = res.data.records.map(itemR => {
          let arrL = this.list.find(itemL => itemL.id === itemR.typeId)
          return { ...itemR, typeName: arrL.typeName }
        })
        this.total = res.data.total;
      })
      // this.$forceUpdate()
    },
    getArticleCategoryList() {
      articleCategoryList().then(res => {
        this.list = res.data;
        this.getArticleList();
      })
    },
    handleSizeChange(val) {
      this.form.page = 1// 重置页码
      this.form.pageSize = val;
      this.getArticleList();
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getArticleList();
    },
    handleDelete(id) {
      // console.log('delete', id);

      this.$confirm('此操作将永久删除该条公告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        articleDelete({ id }).then(res => {
          // console.log(id);

          // console.log(res, '删除');
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getArticleList()
        })


      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleAdd() {
      // console.log('add');
      this.dialogFormVisible = true
    },
    done() {
      this.dialogFormVisible = false;
      this.getArticleList()
    },
    handleEdit(info) {
      this.info = info
      this.dialogFormVisible = true
    }
  },
  mounted() {
    this.getArticleCategoryList()
    // this.getArticleList()
  }
}

</script>
